package com.pocketsweet.ui.uilib.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.pocketsweet.MLApplication;
import com.pocketsweet.R;
import com.pocketsweet.model.MLTopic;
import com.pocketsweet.ui.Topic;
import com.pocketsweet.ui.TopicActivity;
import com.pocketsweet.ui.fragments.RecommendFragment;
import com.pocketsweet.ui.fragments.TempRecommendFragment;
import com.pocketsweet.ui.uilib.MyPagerGalleryView;
import com.pocketsweet.ui.uilib.MyPagerGalleryView.MyOnItemClickListener;

/**
 * @author Kam
 */
public class AdvertiseAdapter extends BaseListAdapter<MLTopic> {
    private List<MLTopic> mlTopic;
    private int likeCount = 0;
    private int i = 0;

    public AdvertiseAdapter(Context ctx) {
        super(ctx);
    }

    public AdvertiseAdapter(Context ctx, List<MLTopic> datas) {
        super(ctx, datas);
    }

    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size() + 1;
        } else {
            return 0;
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder_advertise viewHolder_advertise = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_recommend_advertise,
                    parent, false);
            viewHolder_advertise = new ViewHolder_advertise(convertView);
            getTopic(viewHolder_advertise);
        } else {
            viewHolder_advertise = (ViewHolder_advertise) convertView.getTag();

        }
        return convertView;
    }

    public void getTopic(final ViewHolder_advertise viewHolder_advertise) {
        if (MLApplication.getInstance().getIsLogin() == true) {
            mlTopic = RecommendFragment.getMlTopicList();

        } else {
            return;
        }
        if (mlTopic.size() > 0) {
            final ArrayList<String> ImageList = new ArrayList<String>();// banner图片
            for (int i = 0; i < mlTopic.size(); i++) {
                ImageList.add(mlTopic.get(i).getTitleImgUrl());
            }

            viewHolder_advertise.gallery.start(MLApplication.getContext(),
                    ImageList, 3000, viewHolder_advertise.ovalLayout,
                    R.drawable.dot_focused, R.drawable.dot_normal, null, null);
            viewHolder_advertise.gallery
                    .setMyOnItemClickListener(new MyOnItemClickListener() {
                        public void onItemClick(int curIndex) {
                            switch (mlTopic.get(curIndex).getType()) {
                                case 0:
                                    Intent intent = new Intent(ctx, Topic.class);
                                    intent.putExtra("topic", mlTopic.get(curIndex)
                                            .getObjectId().toString());
                                    intent.putExtra("title", mlTopic.get(curIndex)
                                            .getTitle());
                                    ctx.startActivity(intent);
                                    break;
                                case 1:
                                    Intent intent1 = new Intent(ctx,
                                            TopicActivity.class);
                                    intent1.putExtra("title", mlTopic.get(curIndex)
                                            .getTitle());
                                    intent1.putExtra("url", mlTopic.get(curIndex)
                                            .getAddress());
                                    ctx.startActivity(intent1);
                                    break;
                            }
                            // ToolKits.toast(MLApplication.getContext(), "点击的是"
                            // + curIndex);
                        }
                    });
        }

    }

    public class ViewHolder_advertise implements View.OnClickListener {
        public MyPagerGalleryView gallery;
        public LinearLayout ovalLayout;
        public View parent;

        public ViewHolder_advertise(View parent) {
            this.parent = parent;
            initView();
        }

        public ViewHolder_advertise() {

        }

        public void initView() {
            gallery = (MyPagerGalleryView) parent.findViewById(R.id.adgallery);
            ovalLayout = (LinearLayout) parent.findViewById(R.id.ovalLayout);

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    }
}
